package com.ljkj.blockpay.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;


import androidx.appcompat.app.AppCompatActivity;

import com.ljkj.blockpay.BlockApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by cxf on 2018/6/22.
 */

public class BitmapUtil {

    private static BitmapUtil sInstance;
    private Resources mResources;
    private BitmapFactory.Options mOptions;

    private BitmapUtil() {
        mResources = BlockApplication.context.getResources();
        mOptions = new BitmapFactory.Options();
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        mOptions.inDither = true;
        mOptions.inSampleSize = 1;
    }

    public static BitmapUtil getInstance() {
        if (sInstance == null) {
            synchronized (BitmapUtil.class) {
                if (sInstance == null) {
                    sInstance = new BitmapUtil();
                }
            }
        }
        return sInstance;
    }


//    public Bitmap decodeBitmap(int imgRes) {
//        Bitmap bitmap = null;
//        try {
//            byte[] bytes = IOUtils.toByteArray(String.valueOf(mResources.openRawResource(imgRes)));
//            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, mOptions);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return new SoftReference<>(bitmap).get();
//    }


    public synchronized Bitmap getBitmap(String path) {

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     *    get image from network
     *    @param [String]imageURL
     *    @return [BitMap]image
     */
    public Bitmap returnBitMap(String url) {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    //获取视频的第一帧 然后保存到本地
    public static String saveBitmap(AppCompatActivity me, String uriPath) {
        //获取缩略图
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(uriPath);
        Bitmap bitmap = media.getFrameAtTime();

        String targetPath = me.getFilesDir() + "/beimei/images/";
        if (fileIsExist(targetPath)) {
            File saveFile = new File(targetPath, System.currentTimeMillis() + "");
            try {
                FileOutputStream streamImgOut = new FileOutputStream(saveFile);
                //压缩的意思
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, streamImgOut);
                streamImgOut.flush();
                streamImgOut.close();
                return saveFile.getPath();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        return null;
    }

    static boolean fileIsExist(String fileName) {
        //传入指定的路径，然后判断路径是否存在
        File file = new File(fileName);
        if (file.exists()) {
            return true;
        } else {
            //file.mkdirs 创建文件夹
            return file.mkdirs();
        }
    }


}
